define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'jy/blog/index' + location.search,
                    add_url: 'jy/blog/add',
                    edit_url: 'jy/blog/edit',
                    del_url: 'jy/blog/del',
                    multi_url: 'jy/blog/multi',
                    import_url: 'jy/blog/import',
                    table: 'blog',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'update_time',
                search: false,
                undefinedText: '',
                exportOptions: {
                    fileName: 'export_' + Moment().format("YYYY-MM-DD"),
                    ignoreColumn: ['operate'] //默认不导出第一列(checkbox)与操作(operate)列
                },
                queryParams: function (params) {
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var sort = params.sort;
                    if (sort === 'update_time') {
                        params.sort = 'blog.update_time';
                    }
                    if (filter.status) {
                        filter['blog.status'] = filter.status;
                        op['blog.status'] = op.status;
                        delete filter['status'];
                        delete op['status'];
                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {
                            field: 'avatar',
                            title: __('Avatar'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {
                            field: 'blog_title',
                            title: __('Blog_title'),
                            operate: 'LIKE',
                            cellStyle: {
                                css: {
                                    "max-width": "300px",
                                    "white-space": "nowrap",
                                    "overflow": "hidden",
                                    "text-overflow": "ellipsis"
                                }
                            }
                        },
                        {
                            field: 'sub_title',
                            title: __('Sub_title'),
                            operate: 'LIKE',
                            cellStyle: {
                                css: {
                                    "max-width": "300px",
                                    "white-space": "nowrap",
                                    "overflow": "hidden",
                                    "text-overflow": "ellipsis"
                                }
                            },
                            visible: false
                        },
                        {field: 'text_content', title: __('Text_content'), operate: 'LIKE', visible: false},
                        {
                            field: 'blog_images',
                            title: __('Blog_images'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.images
                        },
                        {field: 'images_count', title: __('Images_count'), operate: false,},
                        {field: 'scan_count', title: __('Scan_count'), operate: false,},
                        {field: 'scan_user_count', title: __('Scan_user_count'), operate: false,},
                        {field: 'share_count', title: __('Share_count'), operate: false,},
                        {field: 'share_user_count', title: __('Share_user_count'), operate: false,},
                        {field: 'praise_count', title: __('Praise_count'), operate: false,},
                        {field: 'praise_user_count', title: __('Praise_user_count'), operate: false,},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"1": __('Status 1'), "2": __('Status 2'), "0": __('Status 0')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'public_time',
                            title: __('Public_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        // {field: 'weigh', title: __('Weigh'), operate: false},
                        {
                            field: 'create_time',
                            title: __('Create_time'),
                            operate: false,
                            addclass: 'datetimerange',
                            autocomplete: false,
                            visible: false,
                        },
                        {
                            field: 'update_time',
                            title: __('Update_time'),
                            operate: false,
                            addclass: 'datetimerange',
                            autocomplete: false,
                            visible: false,
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("select[name='public_user']").change(function () {
                var that = this;
                var type = that.options[that.selectedIndex].value;
                if (type === 'user') {
                    $('#user').show();
                    $('#customize').hide();
                    $("input[name='row[nickname]']").val('');
                    $("input[name='row[avatar]']").val('');
                } else if (type === 'customize') {
                    $("input[name='row[user_id]']").val('');
                    $('#user').hide();
                    $('#customize').show();
                }
            });

            $("select[name='public_type']").change(function () {
                var that = this;
                var type = that.options[that.selectedIndex].value;
                if (type === 'no') {
                    $('#public_time').hide();
                    $("input[name='row[public_time]']").val('');
                } else if (type === 'yes') {
                    $('#public_time').show();
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $("select[name='public_user']").change(function () {
                var that = this;
                var type = that.options[that.selectedIndex].value;
                if (type === 'user') {
                    $('#user').show();
                    $('#customize').hide();
                    $("input[name='row[nickname]']").val('');
                    $("input[name='row[avatar]']").val('');
                } else if (type === 'customize') {
                    $("input[name='row[user_id]']").val('');
                    $("input[name='row[user_id]_text']").val('');
                    $('#user').hide();
                    $('#customize').show();
                }
            });
            $("select[name='public_type']").change(function () {
                var that = this;
                var type = that.options[that.selectedIndex].value;
                if (type === 'no') {
                    $('#public_time').hide();
                    $("input[name='row[public_time]']").val('');
                } else if (type === 'yes') {
                    $('#public_time').show();
                }
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});